Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTabsIndex: 0,
    phoneNumber: '',
    code: '',
    password: '',
    checkPassword: '',
    verification:"",
    companyPhoneNumber:"",
    companyCode:"",
    companyPassword:"",
    checkCompanyPassword:"",
    companyName:"",
    companyPhone:"",
    companyAdd:"",
    codename:"发送验证码",
    companyCode:"发送验证码",
    sencod:60,
    send:true,
    companySend:true
  },
// 选择地址
address:function(){
wx.chooseAddress({
  success:function(res){
    console.log(res)
  }
 
})
},
  /**
   * 获取手机号码
   */
  phoneNumber: function(e) {
    // let reg = /^1\d{10}$/;
    // let phoneNumber = e.detail.value;
    // if (reg.test(phoneNumber) == null){   
    //     
    // }else{
    //     wx.showModal({
    //         title: '提示',
    //         content: '手机号码不对哦',
    //     })
    //     console.log(e.detail.value.match('reg'))
    // }
    this.setData({
      phoneNumber: e.detail.value,
    })
    console.log(this.data.phoneNumber)

  },
  // 个人发送验证码
  sendPersonCode:function(){
    var that=this;
    if ((!(/^((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9]))\d{8}$/.test(that.data.phoneNumber)))){
      wx.showToast({

        title: '手机号码有误',

        duration: 2000,

        icon: "none"

      })
    }else{
      this.setData({
        companySend: false
      })
      this.timer();
    }
    
   
  },
  // 企业发送验证码
  sendCode:function(){
    this.setData({
      companySend: false
    })
    var that = this;
    this.timer();
  },
  timer: function () {
  var that=this;
  var second=60;
  this.setData({
    send:false,
    companySend: false
  })
      let setTimer = setInterval(() => {
        second--;
        that.setData({
          second:second
        })
        that.setData({
          codename: that.data.second + "秒后可重新发送",
          companyCode: that.data.second + "秒后可重新发送"
        })
        console.log(that.data.second);
        if (that.data.second <= 0) {
          that.setData({
            second: 60,
            codename:"重新发送验证码",
            companyCode:"重新发送验证码",
            send:true,
            companySend: true
          })

          clearInterval(setTimer);
         
        }
      }, 1000)
  },
  /**
   * 验证码
   */
  // code: function(e) {
  //   this.setData({
  //       code: e.detail.value,
  //     }),
  //     console.log(this.data.code);
  // },
  num:function(e){
    this.setData({
      verification:e.detail.value
    })
    console.log(this.data.verification);
  },

  /**
   * 密码
   */
  password: function(e) {
    this.setData({
      password: e.detail.value
    })
  },

  checkPassword: function(e) {
    this.setData({
      checkPassword: e.detail.value
    })

  },
  // 获取企业用户号码
  companyPhoneNumber:function(e){
    this.setData({
      companyPhoneNumber:e.detail.value
    })
  },
// 获取企业用户验证码
  companyCode:function(e){
    this.setData({
      companyCode:e.detail.value
    })
  },
  // 获取企业密码
  companyPassword:function(e){
    this.setData({
      companyPassword:e.detail.value
    })
  },
  // 再次输入密码
  checkCompanyPassword:function(e){
    this.setData({
      checkCompanyPassword:e.detail.value
    })
  },
  // 企业名称
  companyName:function(e){
    this.setData({
      companyName:e.detail.value
    })
  },
  companyPhone:function(e){
    this.setData({
      companyPhone:e.detail.value
    })
  },
  companyAdd:function(e){
    this.setData({
      companyAdd:e.detail.value
    })
  },
  /**
   * 注册
   */
  register: function() {
    var that = this;
    if (!(/^((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9]))\d{8}$/.test(that.data.phoneNumber))) {

      wx.showToast({

        title: '手机号码有误',

        duration: 2000,

        icon: "none"

      })
    } else if (that.data.verification == "") {
      wx.showToast({

        title: '请输入验证码',

        duration: 2000,

        icon: "none"

      })
    } else if (this.data.password == "") {
      wx.showToast({

        title: '请输入密码',

        duration: 2000,

        icon: "none"

      })
    } else if (this.data.password == !this.data.checkPassword) {
      wx.showModal({
        title: '提示',
        content: '两次密码不一样哦',
      })

    }else{
    // console.log(this.data.phoneNumber+"lll");
    // wx.request({
    //     url: 'http://192.168.0.103:8002/user/register',
    //     method: "post",
    //     success: function (response){
    //         console.log(response.data)
    //     },
    //     data: {
    //         username: this.data.phoneNumber,
    //         password: this.data.password
    //     }
    // })

    wx.login({
      success: function(res) {
        var code = res.code;
        console.log(code);
        wx.request({
          url: 'https://www.xiaozheng8.com/user/register',
          // url: 'https://www.xiaozheng8.com/user/login',

          method: "post",
          data: {
            code: res.code,
            username: that.data.phoneNumber,
            password: that.data.password
          },
          success: function(res) {
            //返回openid
            console.log(res);
            wx.reLaunch({
              url: '../login/login',
            })
          },
          fail: function(res) {
            console.log(res);
            wx.showModal({
              title: '提示',
              content: '注册失败',
            })
          }
        })
      }

    })
    }
  },
  registerCom:function(){
    var that = this;
    if (companyPhone==!"") {

      wx.showToast({

        title: '手机号码有误',

        duration: 2000,

        icon: "none"

      })
    } else if (that.data.companyCode == "") {
      wx.showToast({

        title: '请输入验证码',

        duration: 2000,

        icon: "none"

      })
    } else if (this.data.companyPassword == "") {
      wx.showToast({

        title: '请输入密码',

        duration: 2000,

        icon: "none"

      })
    } else if (this.data.companyPassword == !this.data.checkCompanyPassword) {
      wx.showModal({
        title: '提示',
        content: '两次密码不一样哦',
      })

    } else if (this.data.companyName==""){

      wx.showModal({
        title: '提示',
        content: '请输入企业名称',
      })
    } else if (this.data.companyPhone == ""){
      wx.showModal({
        title: '提示',
        content: '请输入正确的企业电话',
      })
    } else if (this.data.companyAdd==""){
      wx.showModal({
        title: '提示',
        content: '请输入企业地址',
      })
    }else{

    wx.login({
      success: function (res) {
        var code = res.code;
        console.log(code);
        wx.request({
          url: 'https://www.xiaozheng8.com/user/register',
          // url: 'https://www.xiaozheng8.com/user/login',

          method: "post",
          data: {
            code: res.code,
            username: that.data.phoneNumber,
            password: that.data.password
          },
          success: function (res) {
            //返回openid
            console.log(res)
            wx.reLaunch({
              url: '../login/login',
            })
          },
          fail: function (res) {
            console.log(res);
            wx.showModal({
              title: '提示',
              content: '注册失败',
            })
          }
        })
      }

    })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  code: function() {

  },
  itemTap: function(e) {
    var index = e.currentTarget.dataset.index;
    console.log(index);
    this.setData({
      currentTabsIndex: index
    })
  }
})